#! /bin/sh
# PCP QA Test No. 389
# Exercise pmlogger behaviour when it loses pmcd connection
#
# Copyright (c) 1995-2002 Silicon Graphics, Inc.  All Rights Reserved.
#

seq=`basename $0`
echo "QA output created by $seq"

# get standard filters
. ./common.product
. ./common.filter
. ./common.check

signal=$PCP_BINADM_DIR/pmsignal
host=`hostname`
status=1	# failure is the default!

_filter()
{
    sed -e '/myFetch: End of File: PMCD exited/d' \
	-e '/pmlogger: Error: IPC protocol failure/d'
}

trap "[ -n "$pid" ] && $sudo $PCP_BINADM_DIR/pmsignal -s TERM $pid >/dev/null 2>&1; $sudo rm -f $tmp.*; exit \$status" 0 1 2 3 15
$sudo rm -f $tmp.* $seq.full

# real QA test starts here
echo "=== empty config and _no_ -L, should exit immediately ===" | tee -a $here/$seq.full
$sudo rm -f $tmp.*
pmlogger -c /dev/null -l $tmp.log $tmp &
sleep 2
echo "expect no pmlogger process ..."
ps $PCP_PS_ALL_FLAGS | $PCP_AWK_PROG '$2 == "'$!'" { print }'
_filter_pmlogger_log <$tmp.log | sed -e "s/$host/HOST/"

echo | tee -a $here/$seq.full
echo "=== empty config and -L, no reconnect and no exit ===" | tee -a $here/$seq.full
$sudo rm -f $tmp.*
_start_up_pmlogger -L -c /dev/null -l $tmp.log $tmp
_wait_for_pmlogger $pid $tmp.log 5
_service pcp restart 2>&1 | _filter_pcp_start
_wait_for_pmcd
_wait_for_pmlogger
echo "expect pmlogger process ..."
ps $PCP_PS_ALL_FLAGS | $PCP_AWK_PROG '$2 == "'$pid'" { print "OK"}'
_filter_pmlogger_log <$tmp.log | sed -e "s/$host/HOST/"
$sudo $signal -s TERM $pid

echo | tee -a $here/$seq.full
echo "=== non-empty config, reconnect when pmcd restarted ===" | tee -a $here/$seq.full
# force quick reconnection to pmcd for pmlogger
#
export PMCD_RECONNECT_TIMEOUT=1
$sudo rm -f $tmp.*
echo "log mandatory on 1 sec pmcd.version" >$tmp.config
_start_up_pmlogger -c $tmp.config -l $tmp.log $tmp
_wait_for_pmlogger $pid $tmp.log 5
echo "pid=$pid" >>$here/$seq.full
ps $PCP_PS_ALL_FLAGS | grep '[p]mlogger' | grep $tmp >>$here/$seq.full
_service pmcd stop | _filter_pcp_stop
sleep 2
_service pmcd start 2>&1 | _filter_pcp_start
_wait_for_pmcd
_wait_for_pmlogger
# need longer delay here to allow pmlogger to reconnect and revalidate
# metrics
sleep 4
echo "expect pmlogger process ..." | tee -a $here/$seq.full
ps $PCP_PS_ALL_FLAGS \
| grep '[p]mlogger' \
| grep $tmp \
| tee -a $here/$seq.full \
| $PCP_AWK_PROG '$2 == "'$pid'" { print "OK"}'
_filter_pmlogger_log <$tmp.log | _filter | sed -e "s/$host/HOST/"
$sudo $signal -s TERM $pid
sleep 2
echo "pmlogger log file ..." >>$here/$seq.full
cat $tmp.log >>$here/$seq.full
echo "expect one mark record ..."
pmdumplog -M $tmp | grep '<mark>' | wc -l | sed -e 's/ //g'

# success, all done
status=0
exit
